package com.jeady.timetodo.manager

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.app.NotificationCompat
import com.jeady.timetodo.R
import kotlin.random.Random

const val runningServiceChannelId = "serviceChannel"
const val taskChannelId = "taskChannel"
private val TAG = "[JNotificationManager]"
fun initNotification(context: Context) {
    createNotificationChannel(context, runningServiceChannelId,
        "运行通知", "定时服务运行通知", NotificationManager.IMPORTANCE_HIGH
    )
    createNotificationChannel(context, taskChannelId,
        "任务通知", "任务状态通知", NotificationManager.IMPORTANCE_DEFAULT
    )
}
fun createNotificationChannel(context: Context, channelId: String, channelName: String, channelDescription: String, importance: Int){
    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel(channelId, channelName, importance).apply {
        description = channelDescription
        setShowBadge(true)
        enableVibration(true)
        setAllowBubbles(true)
        enableLights(true)
    }
    manager.createNotificationChannel(channel)
}
fun sendNotification(context: Context, channelId: String, title: String="消息通知", subTitle: String="这是个test通知", clazz: Class<*>? = null){
    val noticeId = Random.nextInt()
    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    manager.notify(noticeId, createNotification(context, channelId, title, subTitle, clazz))
}
fun sendNotification(context: Context, channelId: String, title: String="消息通知", subTitle: String="这是个test通知", intent: Intent? = null){
    val noticeId = Random.nextInt()
    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    manager.notify(noticeId, createNotification(context, channelId, title, subTitle, intent=intent))
}
fun createNotification(
    context: Context, channelId: String, title: String="消息通知",
    subTitle: String="这是个测试通知", cls: Class<*>?=null, intent: Intent?=null
): Notification {
    val newIntent = intent ?: cls?.let {Intent(context, it) }
    val pendingIntent = PendingIntent.getActivity(context, 1002,
        newIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
    return NotificationCompat.Builder(context, channelId)
        .setSmallIcon(R.drawable.future)
        .setContentTitle(title)
        .setContentText(subTitle)
        .setAutoCancel(true)
//        .setStyle(NotificationCompat.BigPictureStyle()
//            .bigPicture(BitmapFactory.decodeResource(context.resources, R.drawable.future))
//        )
        .setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE)
        .setNumber(10)
        .setAllowSystemGeneratedContextualActions(true)
        .setBubbleMetadata(NotificationCompat.BubbleMetadata.fromPlatform(Notification.BubbleMetadata.Builder("a bubble 1").build()))
        .setContentIntent(pendingIntent)
        .build()
}
fun removeAllNotification(context: Context){
    Log.d(TAG, "removeAllNotification: ")
    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//    manager.cancel(noticeId)
    manager.cancelAll()
}